import { _decorator, Component, Vec3, Node } from 'cc';
import { UIWindowOption } from '../ui/SUI';
import { UIMask } from './UIMask';
const { ccclass, property } = _decorator;

/**
 * GUI层的窗口，制作预制时，如果需要定制化的窗口，可以自己创建组件继承它
 */
@ccclass('UIWindow')
export class UIWindow extends Component {
    public options: UIWindowOption = null;
    public mask: UIMask;

    @property(Node)
    public closeNode: Node = null;

    public tweenTime: number = 0.3;

    protected start(): void {
        this.node.scale = Vec3.ZERO;
        this.node.DOScaleTo(this.tweenTime, Vec3.ONE, {
            easing: "backOut"
        }).start();

        if (this.options?.closeOnClickOutside) {
            this.mask.node.OnClick(this.close, this);
        }

        this.closeNode = this.closeNode ?? this.node.getChildByPath("close");
        if (this.closeNode) {
            this.closeNode.OnClick(this.close, this);
        }
    }

    public close() {
        this.node.DOScaleTo(this.tweenTime, Vec3.ZERO, {
            easing: "backIn",
            onComplete: () => {
                if (this.mask) {
                    this.mask.close();
                }

                this.node.destroy();
            }
        }).start();
    }
}